package com.config.tomcat;

import com.config.TomcatServletContextInitializer;
import com.config.tomcat.value.EngineValueTest;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chengyadong
 * @date 2023/9/19 10:15
 * @description webServiceFactory web容器工厂属性自定义，tomcat的启动初始化就是在该工厂中
 * 内嵌tomcat第一步就是获取webServiceFactory容器工厂的bean对象，而在其bean对象的初始化过程中会调用beanPostProcessor
 * 后置处理器，其中WebServerFactoryCustomizerBeanPostProcessor后置处理器就会调用所有实现了WebServerFactoryCustomizer
 * 接口的实现类中的customize方法，从而可以在tomcat各组件创建初始化之前来进行部分功能的自定义
 */
@Component
public class WebServerFactoryCustomizerTest implements WebServerFactoryCustomizer {
	@Override
	public void customize(WebServerFactory factory) {
		//通过该工厂我们可以自定义一些context，engine的value,监听器 还可以将context，Connector，ProtocolHandler等自定义功能添加到工厂长
		TomcatServletWebServerFactory t = (TomcatServletWebServerFactory) factory;
		//通过工厂自定义的方式添加engine组件对应的阀值
		List values = new ArrayList<>();
		values.add(new EngineValueTest());
		t.setEngineValves(values);
		//通过真正的tomcatStarer来调用衍生的ServletContextInitializer，效果相同，但是参数会有出入，只能接收servletContext
		t.addInitializers(new TomcatServletContextInitializer());

		/*t.setInitializers();
		t.setInitParameters();*/
		/*t.addWebListeners();*/
		/*List colc = new ArrayList<>();
		colc.add(new TomcatContextCustomizerTest());
		t.setTomcatContextCustomizers(colc);*/

		/*t.setTomcatConnectorCustomizers();
		t.setTomcatProtocolHandlerCustomizers();*/
	}
}
